package com.ylsk.common.utils;

import com.google.common.base.Joiner;
import com.ylsk.common.constant.CacheConstants;
import com.ylsk.common.core.domain.entity.SysDept;
import com.ylsk.common.core.domain.entity.SysUser;
import com.ylsk.common.core.redis.RedisCache;
import com.ylsk.common.enums.Id2NameType;
import com.ylsk.common.utils.spring.SpringUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class MapCacheContainer {

    private static final Map<Id2NameType, Map<Long, String>> type2Cache = new HashMap<>();

    //缓存组织id-name
    private static Map<Long, String> orgCache = null;
    //缓存人员id-name
    private static Map<Long, String> empCache = null;

    public Map<Long, SysUser> cacheMapEmployee = null;

    public Map<Long, SysDept> cacheMapOrganization = null;

    private static MapCacheContainer mapCacheContainer;

    private static RedisCache redisCache;
    static {
        redisCache = SpringUtils.getBean(RedisCache.class);
    }
    public static MapCacheContainer getInstance() {
        if (mapCacheContainer == null) {
            mapCacheContainer = new MapCacheContainer();
        }
        return mapCacheContainer;
    }

    public MapCacheContainer init(List<SysDept> organizationList, List<SysUser> employeeList) {
        if (CollectionUtils.isNotEmpty(organizationList)) {
            orgCache = organizationList.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
            type2Cache.put(Id2NameType.ORG, orgCache);
            cacheMapOrganization = organizationList.stream().collect(Collectors.toMap(SysDept::getDeptId, Function.identity(), (key1, key2) -> key2));
        }
        if (CollectionUtils.isNotEmpty(employeeList)) {
            empCache = employeeList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName));
            type2Cache.put(Id2NameType.EMP, empCache);
            cacheMapEmployee = employeeList.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity(), (key1, key2) -> key2));
        }
        return mapCacheContainer;

    }

    public MapCacheContainer clean(){
        orgCache = null;
        empCache = null;
        cacheMapEmployee = null;
        cacheMapOrganization = null;
        return mapCacheContainer;
    }


    public String id2Name(Id2NameType id2NameType, String idStr) {
        if (StringUtils.isEmpty(idStr) || id2NameType == null) {
            return StringUtils.EMPTY;
        }
        Map<Long, String> cache = type2Cache.get(id2NameType);
        if (cache == null || cache.isEmpty()) {
            return StringUtils.EMPTY;
        }
        String[] ids = idStr.split(",");
        if (ids.length == 1) {
            return cache.get(ids[0]);
        }
        List<String> ret = new ArrayList<>();
        for (String id : ids) {
            ret.add(cache.get(id));
        }
        return Joiner.on(",").join(ret);
    }

    public String id2Name2(Id2NameType id2NameType, String idStr) {
        if (StringUtils.isEmpty(idStr) || id2NameType == null) {
            return StringUtils.EMPTY;
        }
        if(id2NameType.equals(Id2NameType.EMP)){
            String[] ids = idStr.split(",");
            if (ids.length == 1) {
                return redisCache.getCacheObject(getCacheKey(ids[0]));
            }
            List<String> ret = new ArrayList<>();
            for (String id : ids) {
                ret.add(redisCache.getCacheObject(getCacheKey(id)));
            }
            return Joiner.on(",").join(ret);
        }
        return StringUtils.EMPTY;
    }

    /**
     * 设置cache key
     *
     * @param userKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String userKey)
    {
        return CacheConstants.SYS_USER_KEY + userKey;
    }
}
